package com.taptech.doufu.ui.adapter.personalcenter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.NewPersonalNoteDesActivity;
import com.taptech.doufu.ui.activity.NovelDesToCommActivity;
import com.taptech.doufu.ui.activity.NovelDetailsActivity;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.ui.adapter.BaseListAdapter;
import com.taptech.doufu.bean.ActivitiesBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.services.CartoonServices;
import com.taptech.doufu.ui.activity.SweepFlowerActivity;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.ui.view.RoundImageView;

import java.util.HashMap;

public class PersonalNewDynamicAdapter extends BaseListAdapter {
    public final static int TARGET_TYPE_COMMENT_TOPIC = 2;
    public final static int TARGET_TYPE_NOVEL_SELECT = 4;
    public final static int TARGET_TYPE_SWEEP_COMMENT = 5;
    public final static int TARGET_TYPE_NOVEL_DES = 6;
    public final static int TARGET_TYPE_SWEEP_FLOWER = 7;
    public final static int TARGET_TYPE_COMMENT_TOPIC_COMMENT = 8;
    public final static int TARGET_TYPE_NOVEL_SELECT_COMMENT = 10;
    public final static int TARGET_TYPE_NOVEL_ATRICAL_COMMENT = 29;
    public final static int TARGET_TYPE_ALBMUS = 39; //专辑
    public final static int TARGET_TYPE_NOTE_COMMENT = 41;
    public final static int TARGET_TYPE_DRAW_COMMENT = 42;
    public final static int TARGET_TYPE_COS_COMMENT = 43;


    public final static int TARGET_TYPE_SNNoVel = 74;// 有声小说的评论
    public final static int TARGET_TYPE_SNNoVelAtme = 75; // 评论有声小说并@我
    public final static int TARGET_TYPE_SNNovelReView = 76; //  回复小说评论并@我
    public final static int TARGET_TYPE_SNNOVEL_COMMENT_REPLIES = 77; // 对话小说评论回复列表
    public final static int TARGET_TYPE_SNNOVELCommentAnswer = 78; // 有声小说的回复
    public final static int TARGET_TYPE_NOTE_COMMENT_REPL = 79; // 帖子评论的回复


    private String userHead = "";
    private Handler handler;
    private Activity activity;
    private int target_type;

    public PersonalNewDynamicAdapter(Handler mHandler, Activity activity) {
        // TODO Auto-generated constructor stub
        this.handler = mHandler;
        this.activity = activity;
    }

    public void setUserHead(String userHead) {
        this.userHead = userHead;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.personal_center_new_adapter_dynamic, null);
            viewHolder = new ViewHolder();
            viewHolder.lyView = (View) convertView.findViewById(R.id.personal_centen_new_adapter_dynamic_layout);
            viewHolder.tvDes = (TextView) convertView.findViewById(R.id.personal_centen_new_adapter_dynamic_des);
            viewHolder.tvTime = (TextView) convertView.findViewById(R.id.personal_centen_new_adapter_dynamic_time);
            viewHolder.tvComment = (TextView) convertView.findViewById(R.id.personal_centen_new_adapter_dynamic_comment);
            viewHolder.tvCommentTag = (TextView) convertView.findViewById(R.id.personal_centen_new_adapter_dynamic_comment_tag);
            viewHolder.userHead = (RoundImageView) convertView.findViewById(R.id.personal_centen_new_adapter_dynamic_user_head);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final ActivitiesBean bean = (ActivitiesBean) getDataSource().get(position);
        setTextContent(bean.getDescription(), viewHolder.tvDes);
        setTextContent(DiaobaoUtil.seconds2TimeString(Long.parseLong(bean.getCreated_at())), viewHolder.tvTime);
        viewHolder.lyView.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View arg0) {
                // TODO Auto-generated method stub
                Message message = new Message();
                message.what = target_type;
                message.arg1 = position;
                handler.sendMessage(message);
                // }
                return true;
            }
        });
        setTextContent(bean.getContent(), viewHolder.tvComment);
        setTextContent(bean.getSubobject_title(), viewHolder.tvCommentTag);
        ImageManager.displayImage(viewHolder.userHead, userHead, 1);
        viewHolder.lyView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(final View v) {
                // TODO Auto-generated method stub
                setTagIntent(bean, v.getContext());
            }
        });
        return convertView;
    }


    private void setTextContent(String content, TextView view) {
        if (content != null && !"".equals(content)) {
            view.setText(content);
            view.setVisibility(View.VISIBLE);
        } else {
            view.setVisibility(View.GONE);
        }

    }

    public static class ViewHolder {
        TextView tvDes, tvTime, tvComment, tvCommentTag;
        View lyView;
        RoundImageView userHead;
    }

    /**
     * 动态跳转
     *
     * @param bean
     */
    public void setTagIntent(ActivitiesBean bean, Context mContext) {
        target_type = DiaobaoUtil.String2Int(bean.getTarget_type());
        Intent intent = new Intent();
        switch (target_type) {
            case TARGET_TYPE_NOVEL_DES:
                NovelDetailsActivity.Companion.startActivity(mContext,bean.getObject_id(),Intent.FLAG_ACTIVITY_NEW_TASK);
                break;
            // 小说章节选
            case TARGET_TYPE_NOVEL_SELECT:
            case TARGET_TYPE_NOVEL_SELECT_COMMENT:
                intent.setClass(mContext, NovelDesToCommActivity.class);
                intent.putExtra(Constant.TOPIC, bean.getObject_id());
                intent.putExtra(Constant.ARTICLE_ID, bean.getSubobject_id());
                intent.putExtra(Constant.ARTICLE_TITLE, bean.getSubobject_title());
                intent.putExtra(Constant.FLOWERSHOWFLAG, Constant.FLOWER_HIDE);
                intent.putExtra(Constant.FLAG, true);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
                break;

            case TARGET_TYPE_SWEEP_COMMENT:
            case TARGET_TYPE_SWEEP_FLOWER:
                intent.setClass(mContext, SweepFlowerActivity.class);
                intent.putExtra(Constant.SWEEP_ID, bean.getObject_id());
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
                break;
            case TARGET_TYPE_NOTE_COMMENT_REPL:
            case TARGET_TYPE_NOTE_COMMENT:
                intent.setClass(mContext, SimpleWeexActivity.class);
                intent.putExtra(SimpleWeexActivity.INTENT_WEEX_URL_KEY, "vue/find/TopicDetail/TFTopicDetail.js");
                HashMap<String, String> data = new HashMap<>();
                data.put("id", bean.getObject_id());
                intent.putExtra(SimpleWeexActivity.INTENT_WEEX_DATA_KEY, data);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
                break;
            case TARGET_TYPE_DRAW_COMMENT:
            case TARGET_TYPE_COS_COMMENT:
                intent.setClass(mContext, NewPersonalNoteDesActivity.class);
                intent.putExtra(Constant.OBJECT_TYPE, target_type + "");
                intent.putExtra(Constant.ARTICLE_ID, bean.getObject_id());
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
                break;
            case Constant.OBJECT_TYPE_CARTOON:
                CartoonServices.enterCartoonDes(activity, bean.getObject_id());
                break;
            case TARGET_TYPE_SNNoVel:
            case TARGET_TYPE_SNNoVelAtme:
                HashMap map = new HashMap();
                map.put("novel_id", bean.getObject_id());
                map.put("chapter_id", bean.getSubobject_id());
                SimpleWeexActivity.startActivity(mContext, "vue/speakNovel/comment/WWComment.js", map);
                break;
            case TARGET_TYPE_SNNovelReView:
            case TARGET_TYPE_SNNOVEL_COMMENT_REPLIES:
            case TARGET_TYPE_SNNOVELCommentAnswer:
                HashMap map2 = new HashMap();
                map2.put("id", bean.getParent_id());
                SimpleWeexActivity.startActivity(mContext, "vue/speakNovel/comment/WWCommentDetail.js", map2);
                break;
            case TARGET_TYPE_NOVEL_ATRICAL_COMMENT:
                HashMap map3 = new HashMap();
                map3.put("id", bean.getParent_id());
                map3.put("type", 41);
                SimpleWeexActivity.startActivity(mContext, "TFComment/TFCommentDetail.js", map3);
                break;
            default:
                break;
        }
    }
}
